<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card shadow="never">
        <el-breadcrumb separator-class="el-icon-arrow-right" v-if="$route.query.id">
          <el-breadcrumb-item>学科管理</el-breadcrumb-item>
          <el-breadcrumb-item>{{ names }}</el-breadcrumb-item>
          <el-breadcrumb-item>标签管理</el-breadcrumb-item>
        </el-breadcrumb>
        <hr style="border:none;border:0.2px solid #ccc" />
        <div class="search-box">
          <!-- inline属性： 控制表单项，以水平方向排列 -->
          <el-row type="flex" justify="space-between" align="center" style="height: 50px">
            <el-col :span="20">
              <!-- inline属性： 控制表单项，以水平方向排列 -->
              <el-form :inline="true" class="demo-form-inline">
                <el-form-item label="标签名称">
                  <el-input v-model="params.tagName"></el-input>
                </el-form-item>
                <el-form-item label="状态">
                  <el-select placeholder="请选择" v-model="params.state">
                    <el-option label="启用" value="1"></el-option>
                    <el-option label="禁用" value="0"></el-option>
                  </el-select>
                </el-form-item>
                <el-form-item>
                  <el-button @click="qingchuAn">清除</el-button>
                  <el-button type="primary" @click="sousuoAn">搜索</el-button>
                </el-form-item>
              </el-form>
            </el-col>
            <el-col :span="4">
              <el-row type="flex" justify="end">
                <el-button type="text" @click="fanhuixuekeAn" v-if="$route.query.id"
                  ><i class="el-icon-back"></i>返回学科</el-button
                >
                <el-button size="small" type="success" class="add-btn" @click="xinzengAn"
                  >新增标签</el-button
                >
              </el-row>
            </el-col>
          </el-row>
        </div>
        <el-alert :title="'数据一共' + total + '条'" type="info" show-icon :closable="false">
        </el-alert>
        <!-- 表单 -->
        <el-table :data="tableList" style="width: 100%">
          <el-table-column type="index" label="序号"> </el-table-column>
          <el-table-column prop="subjectName" label="所属学科"> </el-table-column>
          <el-table-column prop="tagName" label="标签名称"> </el-table-column>
          <el-table-column prop="username" label="创建者"> </el-table-column>
          <el-table-column prop="addDate" label="创建日期">
            <template #default="{row}">
              {{ row.addDate | addDateitem }}
            </template>
          </el-table-column>
          <el-table-column prop="state" label="状态">
            <template #default="{row}">
              {{ stateadd(row.state) }}
            </template>
          </el-table-column>
          <el-table-column fixed="right" label="操作" width="180">
            <template #default="{row}">
              <el-button size="small" type="text" @click="jinyongAn(row)">
                <template>
                  {{ row.state ? '禁用' : '启用' }}
                </template>
              </el-button>
              <el-button
                type="text"
                size="small"
                @click="xiugaiAn(row)"
                :disabled="Boolean(row.state)"
                >修改</el-button
              >
              <el-button
                type="text"
                size="small"
                :disabled="Boolean(row.state)"
                @click="shanchuAn(row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="Number(params.page)"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="Number(params.pagesize)"
          layout="->,total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
        <tags-add
          ref="fromadd"
          :dialog-visible="dialogVisible"
          @add="aaaa"
          @update-list="getList"
        ></tags-add>
      </el-card>
    </div>
    <!-- 学科跳转到标签 新增标签对话框 -->
    <el-dialog
      title="新增该学科下的目录"
      append-to-body
      :visible="subjectDialogVisible"
      width="30%"
      @close="closemet"
    >
      <template>
        <el-form label-width="80px" ref="from" :model="idList" :rules="rules">
          <el-form-item label="目录名称" prop="tagName" style="width: 300px">
            <el-input v-model="idList.tagName" />
          </el-form-item>
        </el-form>
      </template>
      <span class="dialog-footer">
        <el-button>取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { list, remove, changeState, add } from '@/api/hmmm/tags.js'
import tagsAdd from '../components/tags-add.vue'
import dayjs from 'dayjs'
export default {
  components: { tagsAdd },
  data () {
    return {
      tableList: [],
      params: {
        page: 1,
        pagesize: 5,
        subjectID: null,
        tagName: null,
        state: null
      },
      total: 0,
      dialogVisible: false,
      formList: {
        subjectName: '',
        tagName: ''
      },
      biaoqianList: [],
      idList: {
        subjectID: '',
        tagName: ''
      },
      names: '',
      subjectDialogVisible: false,
      rules: {
        tagName: [{ required: true, message: '请输入标签名称', trigger: 'blur' }]
      },
      routerId: this.$route.query
    }
  },
  watch: {
    $route: {
      handler () {
        if (this.$route.query.id) {
          this.params.subjectID = this.$route.query.id
          this.idList.subjectID = this.$route.query.id
          // 用于面包屑显示学科名
          this.names = this.$route.query.name
          // 渲染页面
          this.biaoqian()
        } else {
          // params.subjectID初始化，因为下面的方法会用到params，有冲突，必须初始化，否则渲染的就是当前subjectID对应-学科的标签页
          this.params.subjectID = null
          // 重新渲染原始标签页面
          this.getList()
        }
      },
      deep: true,
      immediate: true
    }
  },
  filters: {
    addDateitem (val) {
      return dayjs(val).format('YYYY-MM-DD')
    }
  },
  methods: {
    async getList () {
      const res = await list(this.params)
      this.tableList = res.data.items
      this.total = res.data.counts
    },
    handleSizeChange (val) {
      this.params.pagesize = val
      this.getList()
    },
    handleCurrentChange (val) {
      this.params.page = val
      this.getList()
    },
    xinzengAn () {
      if (this.params.subjectID) {
        this.subjectDialogVisible = true
      } else {
        this.dialogVisible = true
      }
    },
    aaaa () {
      this.dialogVisible = false
    },
    closemet () {
      this.subjectDialogVisible = false
    },
    confirm () {
      this.$refs.from.validate(async valid => {
        if (valid) {
          this.idList.subjectID = Number(this.idList.subjectID)
          await add(this.idList)
          this.$message.success('新增成功')
          // 刷新页面
          this.biaoqian()
          this.$refs.from.resetFields()
          this.closemet()
        }
      })
    },
    xiugaiAn (data) {
      this.dialogVisible = true
      this.$refs.fromadd.fetList(data)
    },
    shanchuAn (data) {
      this.$confirm('确定删除此标签, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          // 删除逻辑
          await remove(data)
          this.$message.success('删除成功')
          // 如果当前页只有1条数据，_page需要-1
          if (this.tableList.length === 1) {
            this.params.page--
          }
          this.getList()
        })
        .catch(() => {})
    },
    stateadd (row) {
      const arr = {
        0: '已禁用',
        1: '已启用'
      }
      return arr[row]
    },
    async jinyongAn (data) {
      if (data.state === 0) {
        await changeState(data, (data.state = 1))
      } else {
        await changeState(data, (data.state = 0))
      }
      this.$message.success('操作成功')
    },
    // 搜索
    async sousuoAn () {
      const res = await list(this.params)
      this.tableList = res.data.items
      this.total = res.data.counts
    },
    qingchuAn () {
      this.params = {
        page: 1,
        pagesize: 10,
        subjectID: null,
        tagName: null,
        state: null
      }
      this.getList()
    },
    fanhuixuekeAn () {
      this.$router.push('/subjects/list')
    },
    // 学科跳转标签
    async biaoqian () {
      const res = await list(this.params)
      this.tableList = res.data.items
      this.total = res.data.counts
    }
  }
}
</script>

<style scoped>
.searvh-box {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
}
.add-btn {
  float: right;
  margin-right: 0px;
}
.el-link {
  margin-left: 550px;
  margin-top: 12px;
}
</style>
